package com.lbj.mall.search.controller;

import com.lbj.mall.common.entity.vo.SkuUpVo;
import com.lbj.mall.common.exception.AppException;
import com.lbj.mall.common.utils.R;
import com.lbj.mall.search.service.ProductSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("search/save")
public class ProductSaveController {

    @Autowired
    private ProductSaveService productSaveService;

    @PostMapping("product")
    public R saveProduct(@RequestBody List<SkuUpVo> skuUpList) {
        boolean result;
        try {
            result = productSaveService.saveProduct(skuUpList);
        } catch (IOException e) {
            log.error("保存商品出错：{}", e.toString());
            return R.error(AppException.ES_SAVE.getCode(), AppException.ES_SAVE.getMsg());
        }
        return result ? R.ok() : R.error(AppException.ES_SAVE.getCode(), AppException.ES_SAVE.getMsg());
    }
}
